
global dat_dir "$base_dir/Data"
global fig_dir "$base_dir/Graphs"

********
*Normal Figure A2-A4*
********

use "$dat_dir/cfda_shock_merged.dta", clear

generate corrected_cfda = string(round(cfda, 0.001))
replace corrected_cfda = corrected_cfda + "0" if strlen(corrected_cfda) == 5
quietly summarize cfda_group
forvalues i = `r(min)'(1)`r(max)' {
	preserve
		keep if cfda_group == `i' & year >= 2000
		local graph_title =
	
		generate log_rd = log(amount_rd)
		generate log_rd_at_shock = log_rd if year == year_shock
		
		quietly summarize log_rd_at_shock
		if `r(N)' == 0 {
			restore
			continue
		}
		
		drop if missing(log_rd)
		
		quietly summarize year
		local min_pt = `r(min)'
		local max_pt = `r(max)'
		
		quietly count
		if `r(N)' <= 9 {
			local step = 1
		}
		else {
			local step = 2
		}
		
		twoway (connected log_rd year) ///
			   (scatter log_rd_at_shock year, mcolor(red) msymbol(Oh) msize(vlarge)), ///
					xscale(r(`min_pt' `max_pt')) ///
					xlabel(`min_pt'(`step')`max_pt') ///
					xtitle("Year") ///
					ytitle("Log Expenditure (millions USD)") ///
					title("`graph_title'", size(medium)) ///
					legend(off) ///
					graphregion(color(white))
		graph export "$fig_dir/cfda_group`i'.pdf", replace
	restore
}

cd "$fig_dir"
local allfiles : dir . files "*.pdf"
local first_eight "cfda_group135.pdf cfda_group263.pdf cfda_group840.pdf cfda_group848.pdf cfda_group1046.pdf cfda_group1055.pdf cfda_group1179.pdf cfda_group1182.pdf"
local allfiles : list allfiles - first_eight
local length : word count `allfiles'

forvalues i = 1(4)8 {
	local file : word `i' of `first_eight'
	
	display "\subfloat{"
	forvalues j = 0(1)3 {
		local index = `i' + `j'
		local file : word `index' of `first_eight'
		display "    \includegraphics[width=0.2475\linewidth]{Graphs/`file'}"
	}
	display "} \\"
}
forvalues i = 1(4)`length' {
	local file : word `i' of `allfiles'
	
	display "\subfloat{"
	forvalues j = 0(1)3 {
		local index = `i' + `j'
		local file : word `index' of `allfiles'
		display "    \includegraphics[width=0.2475\linewidth]{Graphs/`file'}"
	}
	display "} \\"
}

************************
*x Years on Either Side*
************************

use "$dat_dir/cfda_shock_merged.dta", clear

generate corrected_cfda = string(round(cfda, 0.001))
replace corrected_cfda = corrected_cfda + "0" if strlen(corrected_cfda) == 5

quietly summarize year_shock
generate x = .
replace x = `r(min)' - 5 if _n == 1
replace x = `r(max)' + 5 if _n == _N
tsset x
tsfill
generate log_rd = log(amount_rd)
sort cfda_group year
by cfda_group : ipolate log_rd year, generate(log_rd_interp) epolate

quietly summarize cfda_group
forvalues i = `r(min)'(1)`r(max)' {
	foreach offset in 2 3 4 5 {
		if `offset' == 2 {
			local samp_dir "two"
		}
		else if `offset' == 3 {
			local samp_dir "three"
		}
		else if `offset' == 4 {
			local samp_dir "four"
		}
		else if `offset' == 5 {
			local samp_dir "five"
		}
		preserve
			keep if cfda_group == `i'
			keep if inrange(year, year_shock - `offset', year_shock + `offset')
			local graph_title = corrected_cfda[1]
		
			generate log_rd_at_shock = log_rd if year == year_shock
			
			quietly summarize log_rd_at_shock
			if `r(N)' == 0 {
				restore
				continue
			}
			
			quietly summarize year
			local min_pt = `r(min)'
			local max_pt = `r(max)'
			
			display `max_pt'
			
			quietly count
			if `r(N)' <= 10 {
				local step = 1
			}
			else {
				local step = 2
			}
			
			twoway (connected log_rd_interp year) ///
				   (scatter log_rd_at_shock year, mcolor(red) msymbol(Oh) msize(vlarge)), ///
						xscale(r(`min_pt' `max_pt')) ///
						xlabel(`min_pt'(`step')`max_pt') ///
						xtitle("Year") ///
						ytitle("Log Expenditure (millions USD)") ///
						title("`graph_title'", size(medium)) ///
						legend(off) ///
						graphregion(color(white))
			graph export "$fig_dir/`samp_dir'/cfda_group`i'.pdf", replace
		restore
	}
}


cd "$fig_dir/five"
local allfiles : dir . files "*.pdf"
local first_eight "cfda_group135.pdf cfda_group263.pdf cfda_group840.pdf cfda_group848.pdf cfda_group1046.pdf cfda_group1055.pdf cfda_group1179.pdf cfda_group1182.pdf"
local allfiles : list allfiles - first_eight
local length : word count `allfiles'

forvalues i = 1(4)8 {
	local file : word `i' of `first_eight'
	
	display "\subfloat{"
	forvalues j = 0(1)3 {
		local index = `i' + `j'
		local file : word `index' of `first_eight'
		display "    \includegraphics[width=0.2475\linewidth]{Graphs/five/`file'}"
	}
	display "} \\"
}
forvalues i = 1(4)`length' {
	local file : word `i' of `allfiles'
	
	display "\subfloat{"
	forvalues j = 0(1)3 {
		local index = `i' + `j'
		local file : word `index' of `allfiles'
		display "    \includegraphics[width=0.2475\linewidth]{Graphs/five/`file'}"
	}
	display "} \\"
}

****************

cd "$fig_dir/three"
local allfiles : dir . files "*.pdf"
local first_eight "cfda_group135.pdf cfda_group263.pdf cfda_group840.pdf cfda_group848.pdf cfda_group1046.pdf cfda_group1055.pdf cfda_group1179.pdf cfda_group1182.pdf"
local allfiles : list allfiles - first_eight
local allfiles `first_eight' `allfiles'
local length : word count `allfiles'

forvalues i = 1(4)`length' {
	local file : word `i' of `allfiles'
	
	display "\subfloat{"
	forvalues j = 0(1)3 {
		local index = `i' + `j'
		local file : word `index' of `allfiles'
		display "    \includegraphics[width=0.2475\linewidth]{Graphs/three/`file'}"
	}
	display "} \\"
}

cd "$fig_dir/two"
local allfiles : dir . files "*.pdf"
local first_eight "cfda_group135.pdf cfda_group263.pdf cfda_group840.pdf cfda_group848.pdf cfda_group1046.pdf cfda_group1055.pdf cfda_group1179.pdf cfda_group1182.pdf"
local allfiles : list allfiles - first_eight
local allfiles `first_eight' `allfiles'
local length : word count `allfiles'

forvalues i = 1(5)`length' {
	local file : word `i' of `allfiles'
	
	display "\subfloat{"
	forvalues j = 0(1)4 {
		local index = `i' + `j'
		local file : word `index' of `allfiles'
		display "    \includegraphics[width=0.1975\linewidth]{Graphs/two/`file'}"
	}
	display "} \\"
}

cd "$fig_dir/four"
local allfiles : dir . files "*.pdf"
local first_eight "cfda_group135.pdf cfda_group263.pdf cfda_group840.pdf cfda_group848.pdf cfda_group1046.pdf cfda_group1055.pdf cfda_group1179.pdf cfda_group1182.pdf"
local allfiles : list allfiles - first_eight
local allfiles `first_eight' `allfiles'
local length : word count `allfiles'

forvalues i = 1(5)`length' {
	local file : word `i' of `allfiles'
	
	display "\subfloat{"
	forvalues j = 0(1)4 {
		local index = `i' + `j'
		local file : word `index' of `allfiles'
		display "    \includegraphics[width=0.1975\linewidth]{Graphs/four/`file'}"
	}
	display "} \\"
}


